import { Injectable } from '@angular/core';
import { Http, Jsonp } from '@angular/http';
import { Observable } from 'rxjs';
import { Catalogue } from '../../book-catalogue-component/model/catalogues-model';
import { CATALOGUES } from '../../book-catalogue-component/model/catalogues';

@Injectable()
export class CatalogueSearchService {

    source = new Observable(observer => {
        observer.next(CATALOGUES);
    })

    // source = Observable.create(function (observer) {
    //     observer.next(CATALOGUES);
    // })

    constructor() { }

    search(term: string) {
        let result: Catalogue[] = [];
        this.source.subscribe(function (catalogues) {
            let array = catalogues as Catalogue[];
            array.forEach(function (catalogue) {
                if (catalogue.name.indexOf(term) >= 0) {
                    result.push(catalogue);
                }
            })
        });
        return result;
    }
}